package com.boolib.animate.commons.utils;



import org.apache.logging.log4j.util.Strings;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检测工具类
 */
public class CheckUtil {

    public static class Memory{
        public static final Long  M1 = 1048576L;
        public static final Long M2= M1*2;
        public static final Long M10 = M1*10;
        public static final Long M20 = M1*20;
    }
    public static void checkMemberUpload(MultipartFile file){

        if(file.getSize()> Memory.M2){
            throw new RuntimeException("当前单文件上传不得超过2M");
        }
    }



    public static void checkMemberUploads(MultipartRequest request){

        Long  count = 0L;

        List<MultipartFile> files = request.getMultiFileMap().get("files");

        Iterator<MultipartFile> iterator = files.iterator();

        while (iterator.hasNext()){
            MultipartFile next = iterator.next();
            checkMemberUpload(next);
            count+=next.getSize();
        }


        if(count>Memory.M20){
            throw new RuntimeException("多张图片上传不能超过20M");
        }


    }

    public static void checkEmail(String string) {

        if (string == null|| Strings.isBlank(string)) {

            throw new RuntimeException("不能为空");
        }

        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (!m.matches()){
            throw new RuntimeException("邮箱格式不匹配");
        }

    }

    public static boolean isEmail(String string) {

        if (string == null|| Strings.isBlank(string)) {

            throw new RuntimeException("不能为空");
        }

        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (!m.matches()){
           return false;
        }
        return true;

    }




    public static void checkPhone(String string) {
        if (string == null|| Strings.isBlank(string)) {
           throw new RuntimeException("不能为空");
        }



        String regEx1 = "^[1][3,4,5,6,7,8,9][0-9]{9}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (!m.matches()){
            throw new RuntimeException("手机格式不匹配");
        }

    }


    public static boolean isPhone(String string) {
        if (string == null|| Strings.isBlank(string)) {
            throw new RuntimeException("凭证不能为空");
        }



        String regEx1 = "^[1][3,4,5,6,7,8,9][0-9]{9}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (!m.matches()){
            return false;
        }
        return true;

    }



}
